package com.steven.im.utils;

import android.annotation.SuppressLint;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.steven.im.App;

import java.text.SimpleDateFormat;
import java.util.Date;

public class GDLocationUtils implements AMapLocationListener {
    private AMapLocationClient mLocationClient = null;
    private AMapLocationClientOption mLocationOption = null;
    private static GDLocationUtils gdLocationUtils = null;

    public static GDLocationUtils getInstance() {
        if (gdLocationUtils == null) {
            gdLocationUtils = new GDLocationUtils();
        }
        return gdLocationUtils;
    }

    /***
     * 定位
     */
    public void getLocation() {
        try {
            if (mLocationClient == null || mLocationOption == null) {
                mLocationClient = new AMapLocationClient(App.getContext());
                //声明AMapLocationClientOption对象
                mLocationOption = new AMapLocationClientOption();
                mLocationClient.setLocationListener(this);
                //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
                mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
                mLocationOption.setInterval(5000);
                //设置定位参数
                mLocationClient.setLocationOption(mLocationOption);
                //启动定位
                mLocationClient.startLocation();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        try {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //定位成功回调信息，设置相关消息

                    //获取当前定位结果来源，如网络定位结果，详见定位类型表
//                    Log.i("定位类型", amapLocation.getLocationType() + "");
//                    Log.i("获取纬度", amapLocation.getLatitude() + "");
//                    Log.i("获取经度", amapLocation.getLongitude() + "");
//                    Log.i("获取精度信息", amapLocation.getAccuracy() + "");
//
//                    //如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
//                    Log.i("地址", amapLocation.getAddress());
//                    Log.i("国家信息", amapLocation.getCountry());
//                    Log.i("省信息", amapLocation.getProvince());
//                    Log.i("城市信息", amapLocation.getCity());
//                    Log.i("城区信息", amapLocation.getDistrict());
//                    Log.i("街道信息", amapLocation.getStreet());
//                    Log.i("街道门牌号信息", amapLocation.getStreetNum());
//                    Log.i("城市编码", amapLocation.getCityCode());
//                    Log.i("地区编码", amapLocation.getAdCode());
//                    Log.i("获取当前定位点的AOI信息", amapLocation.getAoiName());
//                    Log.i("获取当前室内定位的建筑物Id", amapLocation.getBuildingId());
//                    Log.i("获取当前室内定位的楼层", amapLocation.getFloor());
//                    Log.i("获取GPS的当前状态", amapLocation.getGpsAccuracyStatus() + "");
                    Toast.makeText(App.getContext(),amapLocation.getAddress(),Toast.LENGTH_SHORT).show();
                    //获取定位时间
                    @SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date(amapLocation.getTime());

                    Log.i("获取定位时间", df.format(date));


                    // 停止定位
                    mLocationClient.stopLocation();
                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
